package com.corner.model.cornerservice.utils;

import com.alibaba.fastjson.JSONObject;

import java.security.NoSuchAlgorithmException;
import java.util.Random;

public class DigestUtil {
    public static String getDigest(String imei) throws NoSuchAlgorithmException {
        Random random = new Random();
        int ran = random.nextInt(999999) + 1;
        String ranFormat = String.format("%1$06d", ran);

        long timestamp = System.currentTimeMillis();
        DES des = new DES();
        String imeiDES = des.authcode(imei, "DECODE", "Corner");
        String digest = MD5Util.MD5(imeiDES + ranFormat + timestamp);
        String sign = MD5Util.MD5(imeiDES + digest + timestamp);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("timestamp", timestamp);
        jsonObject.put("imei", imei);
        jsonObject.put("random", ranFormat);
        jsonObject.put("sign", sign);
        return jsonObject.toJSONString();
    }
}
